/**
 * Created by fangshufeng on 2017/12/7.
 */

import React from 'react';
import {Modal, Input, Row, Col} from 'antd'
import jsonFormat from 'json-format'
const {TextArea} = Input;

export  default  class MockJson extends React.Component {


    fixObj = () => {
        let tempObj = this.props.dataSource;
        tempObj.data = this.refs.textarea.textAreaRef.value ;
        tempObj.path = this.refs.path.textAreaRef.value;
        tempObj.name = this.refs.name.textAreaRef.value;
        return  tempObj;
    }

    dataDefaultValue = () => {

        let data =  this.props.dataSource.data;

        if (data === undefined) {
            return "";
        }
        if (data.length === 0) {
            return "";
        }
        try {
            let value = jsonFormat(eval('[' + data + ']')[0], {
                type: 'space',
                size: 2
            });
            return  value;
        }catch (e) {
            return data;
        }
    };
    render() {

        if (Object.keys(this.props.dataSource).length === 0) {
            return (null);
        } else {
            return (

                <Modal
                    title="mock数据"
                    visible={Object.keys(this.props.dataSource).length === 0 ? false : true}
                    onOk={() => this.props.onOk(this.fixObj())}
                    onCancel={() => this.props.hidenAction()}
                    closable={false}
                    okText="确认修改"
                >

                    <p >接口名称:</p>
                    <TextArea ref="name"
                              autosize={true}
                              defaultValue={this.props.dataSource.name}/>
                    <p></p>

                    <p>接口地址:</p>
                    <TextArea ref="path"
                              autosize={true}
                              defaultValue={this.props.dataSource.path}/>
                    <p></p>

                    <p>返回数据:</p>
                    <TextArea ref="textarea"
                              autosize={true}
                              defaultValue={this.dataDefaultValue()}/>
                </Modal>
            );
        }
    }
}